<?PHP
/*
 *	mysql.lib
 *
 *	lebachvu <lebachvu@gmail.com>
 *
 *	This program is sole used by eHomp V.3 system of Kimsnet.
 *	Copyright(c) 2002, Kimsnet Co., Ltd.
 *
 */
#include library system
#if (defined("_MYSQL_LIB_")) return;
#else define("_MYSQL_LIB_", 1);

/*
 *	MySQL class
 *
 *	Provide access methods to mysql.
 */
 
class MySQL {
	var $user, $pass, $host, $db, $id;
	var $result, $rows, $data, $a_rows;

	function MySQL ($host, $user, $pass, $db) {
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->db   = $db;
	}	

	function Connect () {
		global $gMessage, $SERVER_NAME;
		if (!($this->id = @mysql_connect($this->host, $this->user, $this->pass))) {
			$gMessage = "Error(DB Open Error): $this->host : '$SERVER_NAME' : ".mysql_error();
			return false;
		}
		return ($this->SelectDB($this->db));
	}

	function ReConnect(){
		$this->Close();
		$this->Connect();
	}

	function isConnect () {
		if ($this->id) return true;
		else return false;
	}

	function Close () {
		if ($this->isConnect()) {
			@mysql_close($this->id);
			@mysql_free_result($this->result);
		}
	}

	function SelectDB () {
		global $gMessage;
		if (!@mysql_select_db($this->db, $this->id)) {
			$gMessage = "Error(DB Select Error): $this->db : ".mysql_error();
			return false;
		}
		return true;
	}
		
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function Query ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error): $query : ".mysql_error();
			return false;
		}
		$this->rows = @mysql_num_rows($this->result);
		$this->a_rows = @mysql_affected_rows($this->id);
		return true;
	}
	
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function QueryItem ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error): $query : ".mysql_error();
			return false;
		}
		$this->rows = @mysql_num_rows($this->result);
		$this->a_rows = @mysql_affected_rows($this->id);
		if (!($this->data = @mysql_fetch_array($this->result))) {
			$gMessage = "Error(DB Query Error): $query : ".mysql_error();
			return false;
		}
		return($this->data[0]);
	}

	
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function QueryRow ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error): $query : ".mysql_error();
			return false;
		}
		$this->rows = @mysql_num_rows($this->result);
		$this->a_rows = @mysql_affected_rows($this->id);
		if (!($this->data = @mysql_fetch_array($this->result))) {
			$gMessage = "Error(DB Query Error): $query : ".mysql_error();
			return false;
		}
		return($this->data);
	}

	
	/**
	 * Enter description here ...
	 * @param unknown_type $row
	 * @return boolean
	 */
	function Fetch ($row) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!(@mysql_data_seek($this->result, $row))) {
			$gMessage = "Error($row Fetch Error): $row : ".mysql_error();
			return false;
		}
		if (!($this->data = @mysql_fetch_array($this->result))) {
			$gMessage = "Error(DB Fetch Error): $row : ".mysql_error();
			return false;
		}
		return($this->data);
	}

	/**
	 * Enter description here ...
	 * @param unknown_type $row
	 * @return boolean
	 */
	function Fetch_Assoc ($row) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!(@mysql_data_seek($this->result, $row))) {
			$gMessage = "Error($row Fetch Error): $row : ".mysql_error();
			return false;
		}
		if (!($this->data = @mysql_fetch_assoc($this->result))) {
			$gMessage = "Error(DB Fetch Error): $row : ".mysql_error();
			return false;
		}
		return($this->data);
	}

	
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function Insert ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Insert): $query : ".mysql_error();
			return false;
		}
		$this->a_rows = @mysql_affected_rows($this->id);
		return true;
	}

	
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function Update ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Update): $query : ".mysql_error();
			return false;
		}
		$this->a_rows = @mysql_affected_rows($this->id);
		return true;
	}

	
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function Delete ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Delete): $query : ".mysql_error();
			return false;
		}
		$this->a_rows = @mysql_affected_rows($this->id);
		return true;
	}

	# Table 持失(/cfg/Define_Query.php研 凧繕馬食 持失廃陥.);
	/**
	 * Enter description here ...
	 * @param unknown_type $query_name
	 * @param unknown_type $table_no
	 * @return boolean
	 */
	function CreateTable ($query_name, $table_no) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if($query = QueryTrans($query_name)) {
			$query = str_replace('[~TABLE_NAME~]', $table_no, $query);
			if (!($this->result = @mysql_query($query, $this->id))) {
				$gMessage = "Error(Table Create Error): $query : ".mysql_error();
				return false;
			}
		}
		else {
			$gMessage = "Error($query_name)";
			return false;
		}
		return true;
	}

	# 衣引葵聖 溌昔拝 琶推蒸澗 舘授叔楳
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function ExecQuery ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Excute): $query : ".mysql_error();
			return false;
		}
		return true;
	}

	# Table 肢薦(惟獣毒 去拭 督析戚 赤澗 井酔拭澗 Table引澗 紺亀稽 肢薦廃陥.)
	/**
	 * Enter description here ...
	 * @param unknown_type $query
	 * @return boolean
	 */
	function DropTable ($query) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Delete Table): $query : ".mysql_error();
			return false;
		}
		return true;
	}

	# Table 戚 糎仙馬澗走研 端滴廃陥.
	# 糎仙馬檎 true, 糎仙馬走 省生檎 false
	/**
	 * Enter description here ...
	 * @param unknown_type $table_name
	 * @return boolean
	 */
	function isTable ($table_name) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		$this->Query("SHOW TABLES");
		for($i=0; $i<$this->rows; $i++) {
			if($data = $this->Fetch($i)) {
				if($data[0] == $table_name) {
					return true;
					break;
				}
			}
		}
		return false;
	}

	# Table拭 Field亜 糎仙馬澗走研 端滴廃陥.
	# 糎仙馬檎 true, 糎仙馬走 省生檎 false
	/**
	 * Enter description here ...
	 * @param unknown_type $table_name
	 * @param unknown_type $field_name
	 * @return boolean
	 */
	function isField ($table_name, $field_name) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		$this->Query("SHOW FIELDS FROM `$table_name`");
		for($i=0; $i<$this->rows; $i++) {
			if($data = $this->Fetch($i)) {
				if($data[Field] == $field_name) {
					return true;
					break;
				}
			}
		}
		return false;
	}

	# Table 琶球研 蓄亜廃陥.
	#   table_name  : 蓄亜拝 Table Name
	#   fname       : Field Name
	#   ftype       : Field type(VARCHAR, TINYINT, TEXT, DATE, SMALLINT, MEDIUMINT, INT, BIGINT, FLOAT, DOUBLE, DECIMAL,
	#                            DATETIME, TIMESTAMP, TIME, YEAR, CHAR, TINYBLOB, TINYTEXT, BLOB, MEDIUMBLOB, MEDIUMTEXT,
	#                            LONGBLOB, LONGTEXT, ENUM, SET)
	#   flength     : Field Length
	#   fattribute  : 'BINARY' or 'UNSIGNED' or 'UNSIGNED ZEROFILL' or ''
	#   fnull       : 'NULL' or 'NOT NULL'
	#   fdefault    : 奄沙葵 竺舛
	#   fextra      : 'auto_increment' or ''
	#   fkey        : 'primary' or 'index' or 'unique' or ''
	/**
	 * Enter description here ...
	 * @param unknown_type $table_name
	 * @param unknown_type $fname
	 * @param unknown_type $ftype
	 * @param unknown_type $flength
	 * @param unknown_type $fattribute
	 * @param unknown_type $fnull
	 * @param unknown_type $fdefault
	 * @param unknown_type $fextra
	 * @param unknown_type $fkey
	 * @return boolean
	 */
	function AddField ($table_name, $fname, $ftype, $flength, $fattribute, $fnull, $fdefault, $fextra, $fkey) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		$query = "ALTER TABLE `$table_name` ADD `$fname` $ftype";
		if($flength)    $query .= " ($flength)";
		if($fattribute) $query .= " $fattribute";
		if($fdefault)   $query .= " DEFAULT '$fattribute'";
		if($fextra)     $query .= " $fextra";
		if($fnull)      $query .= " $fnull";
		if($fkey)       $query .= " $fkey";

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Add $fname Field): $query : ".mysql_error();
			return false;
		}
		return true;
	}

	# Table 琶球税 失維聖 郊菓陥.
	#   table_name  : 痕井拝 Table Name
	#   foname      : 戚穿税 Field Name
	#   fcname      : 痕井拝 Field Name
	#   ftype       : Field type(VARCHAR, TINYINT, TEXT, DATE, SMALLINT, MEDIUMINT, INT, BIGINT, FLOAT, DOUBLE, DECIMAL,
	#                            DATETIME, TIMESTAMP, TIME, YEAR, CHAR, TINYBLOB, TINYTEXT, BLOB, MEDIUMBLOB, MEDIUMTEXT,
	#                            LONGBLOB, LONGTEXT, ENUM, SET)
	#   flength     : Field Length
	#   fattribute  : 'BINARY', 'UNSIGNED', 'UNSIGNED ZEROFILL'
	#   fnull       : 'NULL' or 'NOT NULL'
	#   fdefault    : 奄沙葵 竺舛
	#   fextra      : 'auto_increment' or ''
	#   fkey        : 'primary' or 'index' or 'unique' or ''
	/**
	 * Enter description here ...
	 * @param unknown_type $table_name
	 * @param unknown_type $foname
	 * @param unknown_type $fcname
	 * @param unknown_type $ftype
	 * @param unknown_type $flength
	 * @param unknown_type $fattribute
	 * @param unknown_type $fnull
	 * @param unknown_type $fdefault
	 * @param unknown_type $fextra
	 * @param unknown_type $fkey
	 * @return boolean
	 */
	function ChangeField ($table_name, $foname, $fcname, $ftype, $flength, $fattribute, $fnull, $fdefault, $fextra, $fkey) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		$query = "ALTER TABLE `$table_name` CHANGE `$foname` `$fcname` $ftype";
		if($flength)    $query .= " ($flength)";
		if($fattribute) $query .= " $fattribute";
		if($fdefault)   $query .= " DEFAULT '$fattribute'";
		if($fextra)     $query .= " $fextra";
		if($fnull)      $query .= " $fnull";
		if($fkey)       $query .= " $fkey";

		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Change $fname Field): $query : ".mysql_error();
			return false;
		}
		return true;
	}

	# Table税 琶球研 肢薦廃陥.
	/**
	 * Enter description here ...
	 * @param unknown_type $table_name
	 * @param unknown_type $fname
	 * @return boolean
	 */
	function DropField ($table_name, $fname) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		$query = "ALTER TABLE `$table_name` DROP `$fname`";
		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Query Error : Delete $fname Field): $query : ".mysql_error();
			return false;
		}
		return true;
	}

	# Table 戚硯聖 痕井廃陥.
	/**
	 * Enter description here ...
	 * @param unknown_type $otable_name
	 * @param unknown_type $ctable_name
	 * @return boolean
	 */
	function ChangeTableName ($otable_name, $ctable_name) {
		global $gMessage;
		if(!$this->isConnect()) $this->Connect();

		$query = "ALTER TABLE `$otable_name` RENAME `$ctable_name`";
		if (!($this->result = @mysql_query($query, $this->id))) {
			$gMessage = "Error(DB Change Error : $otable_name, $ctable_name): $query : ".mysql_error();
			return false;
		}
		return true;
	}

	# 背雁 Table税 Field研 姥廃陥.
	/**
	 * Enter description here ...
	 * @param unknown_type $tablename
	 * @return boolean|Ambigous <multitype:, string>
	 */
	function getFieldName($tablename) {
		global $gMessage;
		$fldName = array();

		if(!$tablename) {
			$gMessage = "Table Name is not exist.";
			return false;
		}

		$fields = mysql_list_fields("$this->db", "$tablename");
		$columns = mysql_num_fields($fields);
		for ($i = 0; $i < $columns; $i++) {
			$fldName[] = mysql_field_name($fields, $i);
		}
		return $fldName;
	}
}
?>